<?php

namespace Qy\Controller;
use Think\Controller;

/**
 *  企业成员
 *  @author zangyilei
 */

class UserController extends QyBaseController {
    
    /**
     *  企业成员列表
     */
    public function index() {
        $strCate_id = I('cate_id',0,'intval');
        $strRole_id = I('role_id',0,'intval');
        $company_id = $this->iCompanyId;
        $role_id    = $this->iRoleId;
        
        if(isset($_POST['cate_id']) != false || isset($_POST['cate_id']) != null){
            $where['qy_user_company_relat.cate_id'] = $strCate_id;
        }
        
        if(isset($_POST['role_id']) != false || isset($_POST['role_id']) != null){
            $where['qy_user_company_relat.role_id'] = $strRole_id;
        }
        
        $where['qy_user_company_relat.company_id'] = $company_id;
        $where['qy_user_company_relat.status'] = array('EGT',0);
        $User = D('UserCompanyRelat');
        $userArr = $User->join('qy_user b on qy_user_company_relat.user_id = b.user_id ')
            ->join('qy_company c on qy_user_company_relat.company_id = c.company_id ')
            ->join('LEFT JOIN qy_category_all d on qy_user_company_relat.cate_id = d.cate_id ')
            ->field('qy_user_company_relat.id as relat_id,
                     b.user_id,
                     b.user_name,
                	 b.phone,
                     b.photo,
                     qy_user_company_relat.status,
                	 qy_user_company_relat.role_id,
                	 d.cate_id,
                	 d.cate_name')
            ->where($where)
            ->order("field(qy_user_company_relat.role_id,1,2,0)")
            ->select();

        if($role_id == 1){ //boss
            
            foreach ($userArr as $key=>&$val){
                $val['photo'] && $val['photo'] = C('SITE_URL').$val['photo'];
                $userArr[$key]['is_edit'] = 1;
                $userArr[$key]['is_remove'] = 1;
                if($val['role_id'] == 1){ 
                    $userArr[$key]['is_edit'] = 0;
                    $userArr[$key]['is_remove'] = 0;
                }
            }
            
        } elseif ($role_id == 2){ //管理员
            
            foreach ($userArr as $key=>&$val){
                $val['photo'] && $val['photo'] = C('SITE_URL').$val['photo'];
                $userArr[$key]['is_edit'] = 1;
                $userArr[$key]['is_remove'] = 1;
                if($val['role_id'] == 1 || $val['role_id'] == 2){ //boss&管理员
                    $userArr[$key]['is_edit'] = 0;
                    $userArr[$key]['is_remove'] = 0;
                }
            }
            
        }else { //普通成员
            
            foreach ($userArr as $key=>&$val){
                $val['photo'] && $val['photo'] = C('SITE_URL').$val['photo'];
                $userArr[$key]['is_edit'] = 0;
                $userArr[$key]['is_remove'] = 0;
            }
            
        } 
//         echo D()->getLastSql();
        $this->returnList($userArr);
        
        
    }
    
    /**
     *  添加企业成员
     */
    public function add() {
        $strPhone = I('phone');
        $strRole_id = I('role_id');
        $strCate_id = I('cate_id');
        $strCompany_id = $this->iCompanyId;
        $Relat = D("UserCompanyRelat");
       
        if ($strRole_id == '请选择角色'){
            $this->error('请选择角色');
        }
        if ($strCate_id == '请选择岗位'){
            $this->error('请选择岗位');
        }
        if(empty($strPhone)) {
            $this->error('请填写手机号码');
        }
        
        $userRes = D('User')->where(array('phone' => $strPhone))->getField('user_id');
        if (empty($userRes)){
            $this->error('该手机号码未注册账号');
        }
        
        $userCount = $Relat->where("role_id = 1 and user_id = $userRes")->count('1');
        if (!empty($userCount)){
            $this->error('该账号为老板，不能添加！');
        }
        
        $relatRes = $Relat->field('id,company_id,status')->where("user_id = $userRes ")->select();
        
        if(!empty($relatRes)){
           
            foreach ($relatRes as $key=>$val){
                
                if($val['company_id'] == $strCompany_id){ //添加本公司
                    
                    if ($val['status'] == -1 || $val['status'] == 2 ){  //退出或过期
                        $Relat = D("UserCompanyRelat");
                        $Relat->id           = $val['id'];
                        $Relat->cate_id      = $strCate_id;
                        $Relat->role_id      = $strRole_id;
                        $Relat->status       = 0;
                        $res                 = $Relat->save();
                        if ($res){
                            $this->success('添加成功！');
                        }else {
                            $this->error('添加失败！');
                        }
                        
                    }else if ($val['status'] == 0 || $val['status'] == 1 ){ //已存在 
                        $this->error('该用户已经加入贵公司!');
                    }
                    
                }else { //添加非本公司
                    
                    if ($val['status'] == 2 ){ //已失效
                        $Relat              = D("UserCompanyRelat");
                        $Relat->id          = $val['id'];
                        $Relat->status      = -1;
                        $res                = $Relat->save();
                    }elseif ($val['status'] == 0 || $val['status'] == 1 ){ //已存在 
                        $this->error('该用户已被其他企业邀请!');
                    }
                    
                }
                
            }   
            
        }
        
        
        if(!$Relat->create()) {
            $this->error($Relat->getError());
        }
        $Relat->user_id      = $userRes;
        $Relat->cate_id      = $strCate_id;
        $Relat->role_id      = $strRole_id;
        $Relat->status       = 0;
        $Relat->company_id   = $strCompany_id;
        $Relat->create_time  = date('Y-m-d H:i:s', time());
        $res                 = $Relat->add();
        if ($res){
            $this->success('添加成功！');
        }else {
            $this->error('添加失败！');
        }
        
    }
    
    /**
     *  确认加入企业
     */
    public function affirm() {
        
        $userID = $this->iUserId;
        $Relat = D('UserCompanyRelat');
        $id = $Relat->where(array('user_id' => $userID,'company_id'=> $this->iCompanyId))->getField('id');
        
        if(empty($id)){
            $this->error('您还未加入任何企业');
        }
        
        $Relat->id           = $id;
        $Relat->status       = 1;
        $res                 = $Relat->save();
        
        $this->success('加入成功！');
    }
    
    /**
     *  编辑成员
     */
    public function edit() {
        $Relat = D("UserCompanyRelat");
        
        $strRelat_id    = I('relat_id',0,'intval');
        $strRole_id     = I('role_id');
        $strCate_id     = I('cate_id');
        
        if(empty($strRelat_id)) {
            $this->error('请正确填写以下信息');
        }
        if($strRole_id == '') {
            $this->error('请选择角色');
        }
        if($strCate_id == '') {
            $this->error('请选择岗位');
        }
        $company_id = $Relat->where(array('id' => $strRelat_id))->getField('company_id');
        
        $Relat->id           = $strRelat_id;
        $Relat->cate_id      = $strCate_id;
        $Relat->company_id   = $company_id;
        $Relat->role_id      = $strRole_id;
        $res                 = $Relat->save();
        
        $this->success('编辑成功！');
    }
    
    /**
     *  成员关联详情
     */
    public function detail() {
        $strRelat_id    = I('relat_id',0,'intval');
        $company_id = $this->iCompanyId;
        
        if(empty($strRelat_id)) {
            $this->error('参数错误');
        }
        
        $where['qy_user_company_relat.company_id'] = $company_id;
        $where['qy_user_company_relat.id'] = $strRelat_id;
        $relatArr = D('UserCompanyRelat')
            ->join('qy_user b on qy_user_company_relat.user_id = b.user_id ')
            ->join('qy_category_all d on qy_user_company_relat.cate_id = d.cate_id ')
            ->field('b.phone,
                     qy_user_company_relat.id,
                     qy_user_company_relat.role_id,
                	 d.cate_id,
                	 d.cate_name'
                )
        	->where($where)
        	->find();
        
       $this->returnList($relatArr);
    }
    
    /**
     *  移除成员
     */
    public function remove() {
        $strRelat_id    = I('relat_id',0,'intval');
        if(empty($strRelat_id)) {
            $this->error('参数错误');
        }
        $Relat = D("UserCompanyRelat");
        $Relat->id           = $strRelat_id;
        $Relat->status       = -1;
        $res                 = $Relat->save();
        
        if ($res){
            $this->success('移除成功！');
        }else {
            $this->error('移除失败！');
        }
    }
    
    /**
     *  岗位列表
     */
    public function cateList() {
        $cate_id = I('cate_id',0,'intval');
        
        $cate_id && $where['cate_id'] = $cate_id;
        $relatArr = D('CategoryAll')
            ->field('cate_id,
                     cate_name
                ')
            ->where('type = 1')
            ->where($where)
            ->select();

        $this->returnList($relatArr);
    }
}